package light.head.dao.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.FileInfo;

import org.springframework.stereotype.Service;

@Service
public class FileDAO extends BaseDAO<FileInfo, Integer> {

	private final static String sql_getByUuid = "FROM FileInfo f WHERE f.uuid = ? AND f.deleted = 0";
	
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean) {
		if (null == oc)
			return RESULT.PARAMETER_ERROR;
		
		
		StringBuffer hql = new StringBuffer("FROM FileInfo f WHERE f.deleted = 0 AND f.oc = ?");
		List<Object> parm = new ArrayList<Object>();
		parm.add(oc);
		if (map != null) {
			String name = map.get(Parm.NAME);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String type = map.get(Parm.TYPE);
			String cu = map.get(Parm.CU);
			String module = map.get(Parm.ATTENDANCE);
			
			if (isNotNull(name)) {
				hql.append(" AND f.name LIKE ?");
				parm.add(name + "%");
			}
			if (isNotNull(time1)) {
				hql.append(" AND f.ct >= ?");
				parm.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if (isNotNull(time2)) {
				hql.append(" AND f.ct <= ?");
				parm.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			if (isNotNull(type)) {
				hql.append(" AND f.type = ?");
				parm.add(Byte.parseByte(type));
			}
			if (isNotNull(cu)) {
				hql.append(" AND f.cu = ?");
				parm.add(Integer.parseInt(cu));
			}
			if (isNotNull(module)) {
				hql.append(" AND f.module = ?");
				parm.add(module);
			}
		}
		
		hql.append(" ORDER BY f.code");
		
		return findToPageBean(hql.toString(), pageBean, parm);
	}
	
	public List<FileInfo> findByOrganCode(Integer organCode, String[] types) {
		StringBuffer sql = new StringBuffer("FROM FileInfo f WHERE deleted <> 1 AND f.organCode = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(organCode);
		if (null != types && types.length > 0) {
			sql.append(" AND f.type IN (");
			for (String type : types) {
				if (isNotNull(type)) {
					sql.append("?,");
					parm.add(type);
				}
			}
			sql.deleteCharAt(sql.length() - 1).append(")");
		}
		List<FileInfo> result = find(sql.toString(), parm.toArray());
		return result;
	}
	
	
	public FileInfo getByUuid(String uuid) {
		if (!isNotNull(uuid)) 
			return null;
		
		List<FileInfo> list = find(sql_getByUuid, uuid);
		if (null == list || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	
	
	public List<FileInfo> getByCodes(List<Integer> codes) {
		if (null == codes || codes.size() == 0) return null;
		StringBuffer sql = new StringBuffer("FROM FileInfo f WHERE f.deleted = 0 AND f.code IN (");
		List<Object> parm = new ArrayList<Object>();
		for (Integer i : codes) {
			sql.append("?,");
			parm.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		return find(sql.toString(), parm.toArray());
	}

	public List<FileInfo> getByCode(Integer code) {
		if (null == code) return null;
		StringBuffer sql = new StringBuffer("FROM FileInfo f WHERE f.deleted = 0 AND f.code = ?");
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		return find(sql.toString(), parm.toArray());
	}
		
		
	
	/**
	 * 通过文件编号集合获取文件信息
	 * @param codes 文件编号集合
	 */
	public List<FileInfo> getByCodes(Integer[] codes) {
		if (null == codes || codes.length == 0) return null;
		StringBuffer sql = new StringBuffer("FROM FileInfo f WHERE f.deleted = 0 AND f.code IN (");
		List<Object> parm = new ArrayList<Object>();
		for (Integer i : codes) {
			sql.append("?,");
			parm.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		return find(sql.toString(), parm.toArray());
	}
}
